package de.gameduell.framework.geom {
	import flash.geom.Point;	

	public class PointMath {

		
		/**
		 * rotates the given vector
		 * 
		 * @param vector the vector you want to rotate
		 * @param angle the angle to rotate the vector
		 * 
		 * @return the rotated vector
		 */
		public static function rotateVector(vector:Point,angle:Number):Point {
			var newX:Number = vector.x * Math.cos(angle) + vector.y * Math.sin(angle);
			var newY:Number = vector.y * Math.cos(angle) - vector.x * Math.sin(angle);
			return new Point(newX, newY);
		}

		/**
		 * multiply the point with the factor and return the result
		 * 
		 * @param aFactor	the factor that will used for the multiplikation
		 */
		public static function multiply(point:Point,aFactor:Number):Point {
			return new Point(point.x * aFactor, point.y * aFactor);
		}

		
		public static function rotatePoint(basePoint:Point, sourcePoint:Point, rotationAngle:int):Point {
			var r:Number;
			var theta:Number;
			var offsetX:Number;
			var offsetY:Number;
			var offsetTheta:Number;
			var rotateX:Number;
			var rotateY:Number;
			var rotationRadians:Number;
			var retPoint:Point;

			//shift x and y relative to 0,0 origin  
			offsetX = (sourcePoint.x + (basePoint.x * -1));
			offsetY = (sourcePoint.y + (basePoint.y * -1));
			//convert to radians. take absolute value (necessary for x coord only).  
			offsetX = Math.abs(offsetX * (Math.PI / 180));
			//offsetX = offsetX * (Math.PI / 180);
	
			//offsetY = Math.abs(offsetY * (Math.PI / 180));
			offsetY = offsetY * (Math.PI / 180);
	
			rotationRadians = rotationAngle * (Math.PI / 180);
			//get distance from origin to source point  
			r = Math.sqrt(Math.pow(offsetX, 2) + Math.pow(offsetY, 2));
			//get current angle of orientation  
			theta = Math.atan(offsetY / offsetX);

			offsetTheta = rotationRadians;
			//calculate new x coord  
			rotateX = r * Math.cos(offsetTheta);
			//calculate new y coord  
			rotateY = r * Math.sin(offsetTheta);
			//convert new x and y back to decimal degrees  
			rotateX = rotateX * (180 / Math.PI);
			rotateY = rotateY * (180 / Math.PI);
			//shift new x and y relative to base point  
			rotateX = (rotateX + basePoint.x);
			rotateY = (rotateY + basePoint.y);
			//return new point  
			retPoint = new Point(rotateX, rotateY);

			return retPoint;
		}
	}
}
